Merge branch 'develop-3.0' of ssh://192.168.1.29/rk/kernel into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rtl8192c / 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 ******************************************************************************/
21 #define _HCI_INTF_C_
22
23 #include <drv_conf.h>
24 #include <osdep_service.h>
25 #include <drv_types.h>
26 #include <recv_osdep.h>
27 #include <xmit_osdep.h>
28 #include <hal_init.h>
29 #include <rtw_version.h>
30
31 #ifndef CONFIG_USB_HCI
32
33 #error "CONFIG_USB_HCI shall be on!\n"
34
35 #endif
36
37 #include "wifi_power.h"
38 #include <usb_vendor_req.h>
39 #include <usb_ops.h>
40 #include <usb_osintf.h>
41 #include <usb_hal.h>
42 #ifdef CONFIG_PLATFORM_RTK_DMP
43 #include <asm/io.h>
44 #endif
45
46 #if defined (PLATFORM_LINUX) && defined (PLATFORM_WINDOWS)
47
48 #error "Shall be Linux or Windows, but not both!\n"
49
50 #endif
51
52 #ifdef CONFIG_80211N_HT
53 extern int rtw_ht_enable;
54 extern int rtw_cbw40_enable;
55 extern int rtw_ampdu_enable;//for enable tx_ampdu
56 #endif
57
58 static struct usb_interface *pintf;
59
60 #ifdef CONFIG_GLOBAL_UI_PID
61 int ui_pid[3] = {0, 0, 0};
62 #endif
63 int sleep_resume = 1;
64
65 extern void rtl8188_power_save_exit(void);
66 extern void rtl8188_power_save_init(void);
67
68 extern int pm_netdev_open(struct net_device *pnetdev,u8 bnormal);
69 static int rtw_suspend(struct usb_interface *intf, pm_message_t message);
70 static int rtw_resume(struct usb_interface *intf);
71 int rtw_resume_process(struct usb_interface *pusb_intf);
72
73
74 static int rtw_drv_init(struct usb_interface *pusb_intf,const struct usb_device_id *pdid);
75 static void rtw_dev_remove(struct usb_interface *pusb_intf);
76
77 #define USB_VENDER_ID_REALTEK           0x0BDA
78
79 //DID_USB_v82_20110808
80 static struct usb_device_id rtw_usb_id_tbl[] ={
81 #ifdef CONFIG_RTL8192C
82         /*=== Realtek demoboard ===*/           
83         {USB_DEVICE(0x0BDA, 0x8191)},//Default ID
84
85         /****** 8188CUS ********/
86         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8176)},//8188cu 1*1 dongole
87         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8170)},//8188CE-VAU USB minCard
88         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817E)},//8188CE-VAU USB minCard
89         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817A)},//8188cu Slim Solo
90         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817B)},//8188cu Slim Combo
91         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817D)},//8188RU High-power USB Dongle
92         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8754)},//8188 Combo for BC4
93         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817F)},//8188RU
94         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x818A)},//RTL8188CUS-VL
95         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x018A)},//RTL8188CTV
96         
97         /****** 8192CUS ********/
98         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8177)},//8191cu 1*2
99         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8178)},//8192cu 2*2
100         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x817C)},//8192CE-VAU USB minCard
101         
102
103         /*=== Customer ID ===*/ 
104         /****** 8188CUS Dongle ********/
105         {USB_DEVICE(0x2019, 0xED17)},//PCI - Edimax
106         {USB_DEVICE(0x0DF6, 0x0052)},//Sitecom - Edimax
107         {USB_DEVICE(0x7392, 0x7811)},//Edimax - Edimax
108         {USB_DEVICE(0x07B8, 0x8189)},//Abocom - Abocom
109         {USB_DEVICE(0x0EB0, 0x9071)},//NO Brand - Etop
110         {USB_DEVICE(0x06F8, 0xE033)},//Hercules - Edimax
111         {USB_DEVICE(0x103C, 0x1629)},//HP - Lite-On ,8188CUS Slim Combo
112         {USB_DEVICE(0x2001, 0x3308)},//D-Link - Alpha
113         {USB_DEVICE(0x050D, 0x1102)},//Belkin - Edimax
114         {USB_DEVICE(0x2019, 0xAB2A)},//Planex - Abocom
115         {USB_DEVICE(0x20F4, 0x648B)},//TRENDnet - Cameo
116         {USB_DEVICE(0x4855, 0x0090)},// - Feixun
117         {USB_DEVICE(0x13D3, 0x3357)},// - AzureWave
118         {USB_DEVICE(0x0DF6, 0x005C)},//Sitecom - Edimax
119         {USB_DEVICE(0x0BDA, 0x5088)},//Thinkware - CC&C
120         {USB_DEVICE(0x4856, 0x0091)},//NetweeN - Feixun
121         {USB_DEVICE(0x2019, 0x4902)},//Planex - Etop
122         {USB_DEVICE(0x2019, 0xAB2E)},//SW-WF02-AD15 -Abocom
123
124         /****** 8188 RU ********/
125         {USB_DEVICE(0x0BDA, 0x317F)},//Netcore,Netcore
126         
127         /****** 8188CE-VAU ********/
128         {USB_DEVICE(0x13D3, 0x3359)},// - Azwave
129         {USB_DEVICE(0x13D3, 0x3358)},// - Azwave
130
131         /****** 8188CUS Slim Solo********/
132         {USB_DEVICE(0x04F2, 0xAFF7)},//XAVI - XAVI
133         {USB_DEVICE(0x04F2, 0xAFF9)},//XAVI - XAVI
134         {USB_DEVICE(0x04F2, 0xAFFA)},//XAVI - XAVI
135
136         /****** 8188CUS Slim Combo ********/
137         {USB_DEVICE(0x04F2, 0xAFF8)},//XAVI - XAVI
138         {USB_DEVICE(0x04F2, 0xAFFB)},//XAVI - XAVI
139         {USB_DEVICE(0x04F2, 0xAFFC)},//XAVI - XAVI
140         {USB_DEVICE(0x2019, 0x1201)},//Planex - Vencer
141         
142         /****** 8192CUS Dongle ********/
143         {USB_DEVICE(0x2001, 0x3307)},//D-Link - Cameo
144         {USB_DEVICE(0x2001, 0x330A)},//D-Link - Alpha
145         {USB_DEVICE(0x2001, 0x3309)},//D-Link - Alpha
146         {USB_DEVICE(0x0586, 0x341F)},//Zyxel - Abocom
147         {USB_DEVICE(0x7392, 0x7822)},//Edimax - Edimax
148         {USB_DEVICE(0x2019, 0xAB2B)},//Planex - Abocom
149         {USB_DEVICE(0x07B8, 0x8178)},//Abocom - Abocom
150         {USB_DEVICE(0x07AA, 0x0056)},//ATKK - Gemtek
151         {USB_DEVICE(0x4855, 0x0091)},// - Feixun
152         {USB_DEVICE(0x2001, 0x3307)},//D-Link-Cameo
153         {USB_DEVICE(0x050D, 0x2102)},//Belkin - Sercomm
154         {USB_DEVICE(0x050D, 0x2103)},//Belkin - Edimax
155         {USB_DEVICE(0x20F4, 0x624D)},//TRENDnet
156         {USB_DEVICE(0x0DF6, 0x0061)},//Sitecom - Edimax
157         {USB_DEVICE(0x0B05, 0x17AB)},//ASUS - Edimax
158         {USB_DEVICE(0x0846, 0x9021)},//Netgear - Sercomm
159         {USB_DEVICE(0x0E66, 0x0019)},//Hawking,Edimax 
160
161         /****** 8192CE-VAU  ********/
162         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8186)},//Intel-Xavi( Azwave)
163 #endif
164 #ifdef CONFIG_RTL8192D
165         /*=== Realtek demoboard ===*/
166         /****** 8192DU ********/
167         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8193)},//8192DU-VC
168         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8194)},//8192DU-VS
169         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x8111)},//Realtek 5G dongle for WiFi Display
170         {USB_DEVICE(USB_VENDER_ID_REALTEK, 0x0193)},//8192DE-VAU
171         
172         /*=== Customer ID ===*/
173         /****** 8192DU-VC ********/
174         {USB_DEVICE(0x2019, 0xAB2C)},//PCI - Abocm
175         {USB_DEVICE(0x2019, 0x4903)},//PCI - ETOP
176         {USB_DEVICE(0x2019, 0x4904)},//PCI - ETOP
177         {USB_DEVICE(0x07B8, 0x8193)},//Abocom - Abocom
178         
179         /****** 8192DU-VS ********/
180         {USB_DEVICE(0x20F4, 0x664B)},//TRENDnet
181
182         /****** 8192DU-WiFi Display Dongle ********/
183         {USB_DEVICE(0x2019, 0xAB2D)},//Planex - Abocom ,5G dongle for WiFi Display
184 #endif
185         {}      /* Terminating entry */
186 };
187
188 int const rtw_usb_id_len = sizeof(rtw_usb_id_tbl) / sizeof(struct usb_device_id);
189
190 static struct specific_device_id specific_device_id_tbl[] = {
191         {.idVendor=USB_VENDER_ID_REALTEK, .idProduct=0x8177, .flags=SPEC_DEV_ID_DISABLE_HT},//8188cu 1*1 dongole, (b/g mode only)
192         {.idVendor=USB_VENDER_ID_REALTEK, .idProduct=0x817E, .flags=SPEC_DEV_ID_DISABLE_HT},//8188CE-VAU USB minCard (b/g mode only)
193         {.idVendor=0x0b05, .idProduct=0x1791, .flags=SPEC_DEV_ID_DISABLE_HT},
194         {.idVendor=0x13D3, .idProduct=0x3311, .flags=SPEC_DEV_ID_DISABLE_HT},
195         {.idVendor=0x13D3, .idProduct=0x3359, .flags=SPEC_DEV_ID_DISABLE_HT},//Russian customer -Azwave (8188CE-VAU  g mode)    
196 #ifdef RTK_DMP_PLATFORM
197         {.idVendor=USB_VENDER_ID_REALTEK, .idProduct=0x8111, .flags=SPEC_DEV_ID_ASSIGN_IFNAME}, // Realtek 5G dongle for WiFi Display
198         {.idVendor=0x2019, .idProduct=0xAB2D, .flags=SPEC_DEV_ID_ASSIGN_IFNAME}, // PCI-Abocom 5G dongle for WiFi Display
199 #endif /* RTK_DMP_PLATFORM */
200         {}
201 };
202
203 typedef struct _driver_priv{
204
205         struct usb_driver rtw_usb_drv;
206         int drv_registered;
207
208 }drv_priv, *pdrv_priv;
209
210
211 static drv_priv drvpriv = {
212         .rtw_usb_drv.name = (char*)DRV_NAME,
213         .rtw_usb_drv.probe = rtw_drv_init,
214         .rtw_usb_drv.disconnect = rtw_dev_remove,
215         .rtw_usb_drv.id_table = rtw_usb_id_tbl,
216         .rtw_usb_drv.suspend =  rtw_suspend,
217         .rtw_usb_drv.resume = rtw_resume,
218 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
219         .rtw_usb_drv.reset_resume   = rtw_resume,
220 #endif
221 #ifdef CONFIG_AUTOSUSPEND       
222         .rtw_usb_drv.supports_autosuspend = 1,  
223 #endif
224 };
225
226 MODULE_DEVICE_TABLE(usb, rtw_usb_id_tbl);
227
228
229 static inline int RT_usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
230 {
231         return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
232 }
233
234 static inline int RT_usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd)
235 {
236         return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
237 }
238
239 static inline int RT_usb_endpoint_xfer_int(const struct usb_endpoint_descriptor *epd)
240 {
241         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT);
242 }
243
244 static inline int RT_usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd)
245 {
246         return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK);
247 }
248
249 static inline int RT_usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd)
250 {
251         return (RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_in(epd));
252 }
253
254 static inline int RT_usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd)
255 {
256         return (RT_usb_endpoint_xfer_bulk(epd) && RT_usb_endpoint_dir_out(epd));
257 }
258
259 static inline int RT_usb_endpoint_is_int_in(const struct usb_endpoint_descriptor *epd)
260 {
261         return (RT_usb_endpoint_xfer_int(epd) && RT_usb_endpoint_dir_in(epd));
262 }
263
264 static inline int RT_usb_endpoint_num(const struct usb_endpoint_descriptor *epd)
265 {
266         return epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
267 }
268
269 u8 rtw_init_intf_priv(_adapter * padapter)
270 {
271         u8 rst = _SUCCESS; 
272         
273         #ifdef CONFIG_USB_VENDOR_REQ_MUTEX
274         _rtw_mutex_init(&padapter->dvobjpriv.usb_vendor_req_mutex);
275         #endif
276
277
278 #ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
279         padapter->dvobjpriv.usb_alloc_vendor_req_buf = rtw_zmalloc(MAX_USB_IO_CTL_SIZE);
280
281         if (padapter->dvobjpriv.usb_alloc_vendor_req_buf   == NULL){
282                 padapter->dvobjpriv.usb_alloc_vendor_req_buf  =NULL;
283                 printk("alloc usb_vendor_req_buf failed... /n");
284                 rst = _FAIL;
285                 goto exit;
286         }
287         padapter->dvobjpriv.usb_vendor_req_buf  = 
288                 (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(padapter->dvobjpriv.usb_alloc_vendor_req_buf ), ALIGNMENT_UNIT);
289 exit:
290 #endif //CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
291
292         return rst;
293         
294 }
295
296 u8 rtw_deinit_intf_priv(_adapter * padapter)
297 {
298         u8 rst = _SUCCESS; 
299
300         #ifdef CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
301         if(padapter->dvobjpriv.usb_vendor_req_buf)
302         {
303                 rtw_mfree(padapter->dvobjpriv.usb_alloc_vendor_req_buf,MAX_USB_IO_CTL_SIZE);
304         }
305         #endif //CONFIG_USB_VENDOR_REQ_BUFFER_PREALLOC
306
307
308         #ifdef CONFIG_USB_VENDOR_REQ_MUTEX
309         _rtw_mutex_free(&padapter->dvobjpriv.usb_vendor_req_mutex);
310         #endif
311         
312         return rst;
313         
314 }
315
316 static u32 usb_dvobj_init(_adapter *padapter)
317 {
318         int     i;
319         u8      val8;
320         int     status = _SUCCESS;
321         struct usb_device_descriptor    *pdev_desc;
322         struct usb_host_config                  *phost_conf;
323         struct usb_config_descriptor            *pconf_desc;
324         struct usb_host_interface               *phost_iface;
325         struct usb_interface_descriptor *piface_desc;
326         struct usb_host_endpoint                *phost_endp;
327         struct usb_endpoint_descriptor  *pendp_desc;
328         struct dvobj_priv       *pdvobjpriv = &padapter->dvobjpriv;
329         struct usb_device       *pusbd = pdvobjpriv->pusbdev;
330
331 _func_enter_;
332
333         pdvobjpriv->padapter = padapter;
334
335         pdvobjpriv->RtNumInPipes = 0;
336         pdvobjpriv->RtNumOutPipes = 0;
337
338         //padapter->EepromAddressSize = 6;
339         //pdvobjpriv->nr_endpoint = 6;
340
341         pdev_desc = &pusbd->descriptor;
342
343 #if 0
344         DBG_8192C("\n8712_usb_device_descriptor:\n");
345         DBG_8192C("bLength=%x\n", pdev_desc->bLength);
346         DBG_8192C("bDescriptorType=%x\n", pdev_desc->bDescriptorType);
347         DBG_8192C("bcdUSB=%x\n", pdev_desc->bcdUSB);
348         DBG_8192C("bDeviceClass=%x\n", pdev_desc->bDeviceClass);
349         DBG_8192C("bDeviceSubClass=%x\n", pdev_desc->bDeviceSubClass);
350         DBG_8192C("bDeviceProtocol=%x\n", pdev_desc->bDeviceProtocol);
351         DBG_8192C("bMaxPacketSize0=%x\n", pdev_desc->bMaxPacketSize0);
352         DBG_8192C("idVendor=%x\n", pdev_desc->idVendor);
353         DBG_8192C("idProduct=%x\n", pdev_desc->idProduct);
354         DBG_8192C("bcdDevice=%x\n", pdev_desc->bcdDevice);
355         DBG_8192C("iManufacturer=%x\n", pdev_desc->iManufacturer);
356         DBG_8192C("iProduct=%x\n", pdev_desc->iProduct);
357         DBG_8192C("iSerialNumber=%x\n", pdev_desc->iSerialNumber);
358         DBG_8192C("bNumConfigurations=%x\n", pdev_desc->bNumConfigurations);
359 #endif
360
361         phost_conf = pusbd->actconfig;
362         pconf_desc = &phost_conf->desc;
363
364 #if 0
365         DBG_8192C("\n8712_usb_configuration_descriptor:\n");
366         DBG_8192C("bLength=%x\n", pconf_desc->bLength);
367         DBG_8192C("bDescriptorType=%x\n", pconf_desc->bDescriptorType);
368         DBG_8192C("wTotalLength=%x\n", pconf_desc->wTotalLength);
369         DBG_8192C("bNumInterfaces=%x\n", pconf_desc->bNumInterfaces);
370         DBG_8192C("bConfigurationValue=%x\n", pconf_desc->bConfigurationValue);
371         DBG_8192C("iConfiguration=%x\n", pconf_desc->iConfiguration);
372         DBG_8192C("bmAttributes=%x\n", pconf_desc->bmAttributes);
373         DBG_8192C("bMaxPower=%x\n", pconf_desc->bMaxPower);
374 #endif
375
376         //DBG_8192C("\n/****** num of altsetting = (%d) ******/\n", pintf->num_altsetting);
377
378         phost_iface = &pintf->altsetting[0];
379         piface_desc = &phost_iface->desc;
380
381 #if 0
382         DBG_8192C("\n8712_usb_interface_descriptor:\n");
383         DBG_8192C("bLength=%x\n", piface_desc->bLength);
384         DBG_8192C("bDescriptorType=%x\n", piface_desc->bDescriptorType);
385         DBG_8192C("bInterfaceNumber=%x\n", piface_desc->bInterfaceNumber);
386         DBG_8192C("bAlternateSetting=%x\n", piface_desc->bAlternateSetting);
387         DBG_8192C("bNumEndpoints=%x\n", piface_desc->bNumEndpoints);
388         DBG_8192C("bInterfaceClass=%x\n", piface_desc->bInterfaceClass);
389         DBG_8192C("bInterfaceSubClass=%x\n", piface_desc->bInterfaceSubClass);
390         DBG_8192C("bInterfaceProtocol=%x\n", piface_desc->bInterfaceProtocol);
391         DBG_8192C("iInterface=%x\n", piface_desc->iInterface);
392 #endif
393
394         pdvobjpriv->NumInterfaces = pconf_desc->bNumInterfaces;
395         pdvobjpriv->InterfaceNumber = piface_desc->bInterfaceNumber;
396         pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
397
398         //DBG_8192C("\ndump usb_endpoint_descriptor:\n");
399
400         for (i = 0; i < pdvobjpriv->nr_endpoint; i++)
401         {
402                 phost_endp = phost_iface->endpoint + i;
403                 if (phost_endp)
404                 {
405                         pendp_desc = &phost_endp->desc;
406
407                         DBG_8192C("\nusb_endpoint_descriptor(%d):\n", i);
408                         DBG_8192C("bLength=%x\n",pendp_desc->bLength);
409                         DBG_8192C("bDescriptorType=%x\n",pendp_desc->bDescriptorType);
410                         DBG_8192C("bEndpointAddress=%x\n",pendp_desc->bEndpointAddress);
411                         //DBG_8192C("bmAttributes=%x\n",pendp_desc->bmAttributes);
412                         //DBG_8192C("wMaxPacketSize=%x\n",pendp_desc->wMaxPacketSize);
413                         DBG_8192C("wMaxPacketSize=%x\n",le16_to_cpu(pendp_desc->wMaxPacketSize));
414                         DBG_8192C("bInterval=%x\n",pendp_desc->bInterval);
415                         //DBG_8192C("bRefresh=%x\n",pendp_desc->bRefresh);
416                         //DBG_8192C("bSynchAddress=%x\n",pendp_desc->bSynchAddress);
417
418                         if (RT_usb_endpoint_is_bulk_in(pendp_desc))
419                         {
420                                 DBG_8192C("RT_usb_endpoint_is_bulk_in = %x\n", RT_usb_endpoint_num(pendp_desc));
421                                 pdvobjpriv->RtNumInPipes++;
422                         }
423                         else if (RT_usb_endpoint_is_int_in(pendp_desc))
424                         {
425                                 DBG_8192C("RT_usb_endpoint_is_int_in = %x, Interval = %x\n", RT_usb_endpoint_num(pendp_desc),pendp_desc->bInterval);
426                                 pdvobjpriv->RtNumInPipes++;
427                         }
428                         else if (RT_usb_endpoint_is_bulk_out(pendp_desc))
429                         {
430                                 DBG_8192C("RT_usb_endpoint_is_bulk_out = %x\n", RT_usb_endpoint_num(pendp_desc));
431                                 pdvobjpriv->RtNumOutPipes++;
432                         }
433                         pdvobjpriv->ep_num[i] = RT_usb_endpoint_num(pendp_desc);
434                 }
435         }
436         
437         DBG_8192C("nr_endpoint=%d, in_num=%d, out_num=%d\n\n", pdvobjpriv->nr_endpoint, pdvobjpriv->RtNumInPipes, pdvobjpriv->RtNumOutPipes);
438
439         if (pusbd->speed == USB_SPEED_HIGH)
440         {
441                 pdvobjpriv->ishighspeed = _TRUE;
442                 DBG_8192C("USB_SPEED_HIGH\n");
443         }
444         else
445         {
446                 pdvobjpriv->ishighspeed = _FALSE;
447                 DBG_8192C("NON USB_SPEED_HIGH\n");
448         }
449
450         //.2
451         if ((rtw_init_io_priv(padapter)) == _FAIL)
452         {
453                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,(" \n Can't init io_reqs\n"));
454                 status = _FAIL;
455         }
456         
457         if((rtw_init_intf_priv(padapter) )== _FAIL)
458         {
459                 RT_TRACE(_module_os_intfs_c_,_drv_err_,("\n Can't INIT rtw_init_intf_priv\n"));
460                 status = _FAIL;
461         }
462
463         //.3 misc
464         _rtw_init_sema(&(padapter->dvobjpriv.usb_suspend_sema), 0);     
465
466         intf_read_chip_version(padapter);
467
468         //.4 usb endpoint mapping
469         intf_chip_configure(padapter);
470
471         rtw_reset_continual_urb_error(pdvobjpriv);
472         
473 _func_exit_;
474
475         return status;
476 }
477
478 static void usb_dvobj_deinit(_adapter * padapter){
479
480         struct dvobj_priv *pdvobjpriv=&padapter->dvobjpriv;
481
482         _func_enter_;
483
484         rtw_deinit_intf_priv(padapter);
485
486         _func_exit_;
487 }
488
489 static void decide_chip_type_by_usb_device_id(_adapter *padapter, const struct usb_device_id *pdid)
490 {
491         //u32   i;
492         //u16   vid, pid;
493
494         padapter->chip_type = NULL_CHIP_TYPE;
495
496         //vid = pdid->idVendor;
497         //pid = pdid->idProduct;
498
499         //TODO: dynamic judge 92c or 92d according to usb vid and pid.
500 #ifdef CONFIG_RTL8192C
501         padapter->chip_type = RTL8188C_8192C;
502         padapter->HardwareType = HARDWARE_TYPE_RTL8192CU;
503         DBG_8192C("CHIP TYPE: RTL8188C_8192C\n");
504 #endif
505
506 #ifdef CONFIG_RTL8192D
507         padapter->chip_type = RTL8192D;
508         padapter->HardwareType = HARDWARE_TYPE_RTL8192DU;
509         DBG_8192C("CHIP TYPE: RTL8192D\n");
510 #endif
511
512 }
513
514 static void usb_intf_start(_adapter *padapter)
515 {
516
517         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+usb_intf_start\n"));
518
519         if(padapter->HalFunc.inirp_init == NULL)
520         {
521                 RT_TRACE(_module_os_intfs_c_,_drv_err_,("Initialize dvobjpriv.inirp_init error!!!\n"));
522         }
523         else
524         {       
525                 padapter->HalFunc.inirp_init(padapter);
526         }                       
527
528         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-usb_intf_start\n"));
529
530 }
531
532 static void usb_intf_stop(_adapter *padapter)
533 {
534
535         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+usb_intf_stop\n"));
536
537         //disabel_hw_interrupt
538         if(padapter->bSurpriseRemoved == _FALSE)
539         {
540                 //device still exists, so driver can do i/o operation
541                 //TODO:
542                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("SurpriseRemoved==_FALSE\n"));
543         }
544
545         //cancel in irp
546         if(padapter->HalFunc.inirp_deinit !=NULL)
547         {
548                 padapter->HalFunc.inirp_deinit(padapter);
549         }
550
551         //cancel out irp
552         rtw_write_port_cancel(padapter);
553
554         //todo:cancel other irps
555
556         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-usb_intf_stop\n"));
557
558 }
559
560 static void rtw_dev_unload(_adapter *padapter)
561 {
562         struct net_device *pnetdev= (struct net_device*)padapter->pnetdev;
563         u8 val8;
564         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+rtw_dev_unload\n"));
565
566         if(padapter->bup == _TRUE)
567         {
568                 DBG_8192C("===> rtw_dev_unload\n");
569
570                 padapter->bDriverStopped = _TRUE;
571
572                 //s3.
573                 if(padapter->intf_stop)
574                 {
575                         padapter->intf_stop(padapter);
576                 }
577
578                 //s4.
579                 if(!padapter->pwrctrlpriv.bInternalAutoSuspend )                        
580                 rtw_stop_drv_threads(padapter);
581
582
583                 //s5.
584                 if(padapter->bSurpriseRemoved == _FALSE)
585                 {
586                         //DBG_8192C("r871x_dev_unload()->rtl871x_hal_deinit()\n");
587                         #ifdef CONFIG_WOWLAN
588                         if(padapter->pwrctrlpriv.bSupportWakeOnWlan==_TRUE){
589                                 DBG_8192C("%s bSupportWakeOnWlan==_TRUE  do not run rtw_hal_deinit()\n",__FUNCTION__);
590                         }
591                         else
592                         #endif
593                         {
594                                 rtw_hal_deinit(padapter);
595                         }
596                         padapter->bSurpriseRemoved = _TRUE;
597                 }
598
599                 padapter->bup = _FALSE;
600
601         }
602         else
603         {
604                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("r871x_dev_unload():padapter->bup == _FALSE\n" ));
605         }
606
607         DBG_8192C("<=== rtw_dev_unload\n");
608
609         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-rtw_dev_unload\n"));
610
611 }
612
613 static void process_spec_devid(const struct usb_device_id *pdid)
614 {
615         u16 vid, pid;
616         u32 flags;
617         int i;
618         int num = sizeof(specific_device_id_tbl)/sizeof(struct specific_device_id);
619
620         for(i=0; i<num; i++)
621         {
622                 vid = specific_device_id_tbl[i].idVendor;
623                 pid = specific_device_id_tbl[i].idProduct;
624                 flags = specific_device_id_tbl[i].flags;
625
626 #ifdef CONFIG_80211N_HT
627                 if((pdid->idVendor==vid) && (pdid->idProduct==pid) && (flags&SPEC_DEV_ID_DISABLE_HT))
628                 {
629                          rtw_ht_enable = 0;
630                          rtw_cbw40_enable = 0;
631                          rtw_ampdu_enable = 0;
632                 }
633 #endif
634
635 #ifdef RTK_DMP_PLATFORM
636                 // Change the ifname to wlan10 when PC side WFD dongle plugin on DMP platform.
637                 // It is used to distinguish between normal and PC-side wifi dongle/module.
638                 if((pdid->idVendor==vid) && (pdid->idProduct==pid) && (flags&SPEC_DEV_ID_ASSIGN_IFNAME))
639                 {
640                         extern char* ifname;
641                         strncpy(ifname, "wlan10", 6); 
642                         //DBG_8192C("%s()-%d: ifname=%s, vid=%04X, pid=%04X\n", __FUNCTION__, __LINE__, ifname, vid, pid);
643                 }
644 #endif /* RTK_DMP_PLATFORM */
645
646         }
647 }
648
649 #ifdef SUPPORT_HW_RFOFF_DETECTED
650 extern u8 disconnect_hdl(_adapter *padapter, u8 *pbuf);
651 extern void rtw_os_indicate_disconnect( _adapter *adapter );
652
653 int rtw_hw_suspend(_adapter *padapter )
654 {
655         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
656         struct usb_interface *pusb_intf = padapter->dvobjpriv.pusbintf; 
657         struct net_device *pnetdev=usb_get_intfdata(pusb_intf);
658         
659         _func_enter_;
660
661         if((!padapter->bup) || (padapter->bDriverStopped)||(padapter->bSurpriseRemoved))
662         {
663                 DBG_8192C("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
664                         padapter->bup, padapter->bDriverStopped,padapter->bSurpriseRemoved);            
665                 goto error_exit;
666         }
667         
668         if(padapter)//system suspend
669         {               
670                 LeaveAllPowerSaveMode(padapter);
671                 
672                 DBG_8192C("==> rtw_hw_suspend\n");      
673                 _enter_pwrlock(&pwrpriv->lock);
674                 pwrpriv->bips_processing = _TRUE;
675                 //padapter->net_closed = _TRUE;
676                 //s1.
677                 if(pnetdev)
678                 {
679                         netif_carrier_off(pnetdev);
680                         netif_stop_queue(pnetdev);
681                 }
682
683                 //s2.
684                 //s2-1.  issue rtw_disassoc_cmd to fw
685                 //rtw_disassoc_cmd(padapter);//donnot enqueue cmd
686                 disconnect_hdl(padapter, NULL);
687                 
688                 //s2-2.  indicate disconnect to os
689                 //rtw_indicate_disconnect(padapter);
690                 {
691                         struct  mlme_priv *pmlmepriv = &padapter->mlmepriv;             
692
693                         if(check_fwstate(pmlmepriv, _FW_LINKED))
694                         {
695                                 _clr_fwstate_(pmlmepriv, _FW_LINKED);
696
697                                 rtw_led_control(padapter, LED_CTL_NO_LINK);
698
699                                 rtw_os_indicate_disconnect(padapter);
700                                 
701                                 #ifdef CONFIG_LPS
702                                 //donnot enqueue cmd
703                                 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 0);
704                                 #endif
705                         }
706
707                 }
708                 //s2-3.
709                 rtw_free_assoc_resources(padapter);
710
711                 //s2-4.
712                 rtw_free_network_queue(padapter,_TRUE);
713                 #ifdef CONFIG_IPS
714                 rtw_ips_dev_unload(padapter);                   
715                 #endif 
716                 pwrpriv->rf_pwrstate = rf_off;
717                 pwrpriv->bips_processing = _FALSE;              
718
719                 _exit_pwrlock(&pwrpriv->lock);
720         }
721         else
722                 goto error_exit;
723         
724         _func_exit_;
725         return 0;
726         
727 error_exit:
728         DBG_8192C("%s, failed \n",__FUNCTION__);
729         return (-1);
730
731 }
732
733 int rtw_hw_resume(_adapter *padapter)
734 {
735         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
736         struct usb_interface *pusb_intf = padapter->dvobjpriv.pusbintf;
737         struct net_device *pnetdev=usb_get_intfdata(pusb_intf); 
738
739         _func_enter_;
740
741         if(padapter)//system resume
742         {       
743                 DBG_8192C("==> rtw_hw_resume\n");
744                 _enter_pwrlock(&pwrpriv->lock);
745                 pwrpriv->bips_processing = _TRUE;
746                 rtw_reset_drv_sw(padapter);
747         
748                 if(pm_netdev_open(pnetdev,_FALSE) != 0)
749                 {
750                         _exit_pwrlock(&pwrpriv->lock);
751                         goto error_exit;
752                 }
753
754                 netif_device_attach(pnetdev);   
755                 netif_carrier_on(pnetdev);
756
757                 if(!netif_queue_stopped(pnetdev))
758                         netif_start_queue(pnetdev);
759                 else
760                         netif_wake_queue(pnetdev);
761                 
762                 pwrpriv->bkeepfwalive = _FALSE;
763                 pwrpriv->brfoffbyhw = _FALSE;
764                 
765                 pwrpriv->rf_pwrstate = rf_on;
766                 pwrpriv->bips_processing = _FALSE;      
767         
768                 _exit_pwrlock(&pwrpriv->lock);
769         }
770         else
771         {
772                 goto error_exit;        
773         }
774
775         _func_exit_;
776         
777         return 0;
778 error_exit:
779         DBG_8192C("%s, Open net dev failed \n",__FUNCTION__);
780         return (-1);
781 }
782 #endif
783
784 static int rtw_suspend(struct usb_interface *pusb_intf, pm_message_t message)
785 {
786         struct net_device *pnetdev=usb_get_intfdata(pusb_intf);
787         _adapter *padapter = (_adapter*)rtw_netdev_priv(pnetdev);
788         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
789         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
790         struct usb_device *usb_dev = interface_to_usbdev(pusb_intf);
791         
792         _func_enter_;
793
794         if((!padapter->bup) || (padapter->bDriverStopped)||(padapter->bSurpriseRemoved))
795         {
796                 DBG_8192C("padapter->bup=%d bDriverStopped=%d bSurpriseRemoved = %d\n",
797                         padapter->bup, padapter->bDriverStopped,padapter->bSurpriseRemoved);                            
798                 return 0;
799         }       
800         sleep_resume = 0;
801         DBG_8192C("###########  rtw_suspend  #################\n");
802         
803         if(padapter)//system suspend
804         {       
805                 if(pwrpriv->bInternalAutoSuspend )
806                 {
807                 #ifdef CONFIG_AUTOSUSPEND       
808                 #ifdef SUPPORT_HW_RFOFF_DETECTED
809                         // The FW command register update must after MAC and FW init ready.
810                         if((padapter->bFWReady) && ( padapter->pwrctrlpriv.bHWPwrPindetect ) && (padapter->registrypriv.usbss_enable ))
811                         {
812                                 u8 bOpen = _TRUE;
813                                 rtw_interface_ps_func(padapter,HAL_USB_SELECT_SUSPEND,&bOpen);
814                                 //rtl8192c_set_FwSelectSuspend_cmd(padapter,_TRUE ,500);//note fw to support hw power down ping detect
815                         }
816                 #endif
817                 #endif
818                 }
819                 pwrpriv->bInSuspend = _TRUE;            
820                 rtw_cancel_all_timer(padapter);         
821                 LeaveAllPowerSaveMode(padapter);
822
823                 _enter_pwrlock(&pwrpriv->lock);
824                 //padapter->net_closed = _TRUE;
825                 //s1.
826                 if(pnetdev)
827                 {
828                         netif_carrier_off(pnetdev);
829                         netif_stop_queue(pnetdev);
830                 }
831 #ifdef CONFIG_WOWLAN
832                 padapter->pwrctrlpriv.bSupportWakeOnWlan=_TRUE;
833 #else           
834                 //s2.
835                 //s2-1.  issue rtw_disassoc_cmd to fw
836                 disconnect_hdl(padapter, NULL);
837                 //rtw_disassoc_cmd(padapter);
838 #endif
839
840 #ifdef CONFIG_LAYER2_ROAMING_RESUME
841                 if(check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED) )
842                 {
843                         //printk("%s:%d assoc_ssid:%s\n", __FUNCTION__, __LINE__, pmlmepriv->assoc_ssid.Ssid);
844                         DBG_871X("%s:%d %s(" MAC_FMT "), length:%d assoc_ssid.length:%d\n",__FUNCTION__, __LINE__,
845                                         pmlmepriv->cur_network.network.Ssid.Ssid,
846                                         MAC_ARG(pmlmepriv->cur_network.network.MacAddress),
847                                         pmlmepriv->cur_network.network.Ssid.SsidLength,
848                                         pmlmepriv->assoc_ssid.SsidLength);
849                         
850                         pmlmepriv->to_roaming = 1;
851                 }
852 #endif
853                 //s2-2.  indicate disconnect to os
854                 rtw_indicate_disconnect(padapter);
855                 //s2-3.
856                 rtw_free_assoc_resources(padapter);
857 #ifdef CONFIG_AUTOSUSPEND
858                 if(!pwrpriv->bInternalAutoSuspend )
859 #endif
860                 //s2-4.
861                 rtw_free_network_queue(padapter, _TRUE);
862
863                 rtw_dev_unload(padapter);
864 #ifdef CONFIG_AUTOSUSPEND
865                 pwrpriv->rf_pwrstate = rf_off;
866                 pwrpriv->bips_processing = _FALSE;
867 #endif
868                 _exit_pwrlock(&pwrpriv->lock);
869         }
870         else
871                 goto error_exit;
872         
873         DBG_871X("###########  rtw_suspend  done #################\n");
874
875         _func_exit_;
876         return 0;
877         
878 error_exit:
879         DBG_871X("###########  rtw_suspend  fail !! #################\n");
880         return (-1);
881
882 }
883
884 static int rtw_resume(struct usb_interface *pusb_intf)
885 {
886         struct net_device *pnetdev=usb_get_intfdata(pusb_intf);
887         _adapter *padapter = (_adapter*)rtw_netdev_priv(pnetdev);
888         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
889          int ret = 0;
890  
891         if(pwrpriv->bInternalAutoSuspend ){
892                 ret = rtw_resume_process(pusb_intf);
893         } else {
894 #ifdef CONFIG_RESUME_IN_WORKQUEUE
895                 rtw_resume_in_workqueue(pwrpriv);
896 #elif defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
897                 if(rtw_is_earlysuspend_registered(pwrpriv)) {
898                         //jeff: bypass resume here, do in late_resume
899                         pwrpriv->do_late_resume = _TRUE;
900                 } else {
901                         ret = rtw_resume_process(pusb_intf);
902                 }
903 #else // Normal resume process
904                 ret = rtw_resume_process(pusb_intf);
905 #endif //CONFIG_RESUME_IN_WORKQUEUE
906         }
907         
908         return ret;
909
910 }
911
912
913 int rtw_resume_process(struct usb_interface *pusb_intf)
914 {
915         struct net_device *pnetdev;
916         struct usb_device *usb_dev;
917         _adapter *padapter;
918         struct pwrctrl_priv *pwrpriv;
919         
920         _func_enter_;
921
922         DBG_8192C("###########  rtw_resume  #################\n");
923
924         if(pusb_intf) {
925                 pnetdev=usb_get_intfdata(pusb_intf);
926                 usb_dev = interface_to_usbdev(pusb_intf);
927         } else {
928                 goto error_exit;
929         }
930
931         padapter = (_adapter*)rtw_netdev_priv(pnetdev);
932         pwrpriv = &padapter->pwrctrlpriv;       
933         
934         if(padapter)//system resume
935         {
936                 _enter_pwrlock(&pwrpriv->lock);
937                 rtw_reset_drv_sw(padapter);
938                 pwrpriv->bkeepfwalive = _FALSE;
939                 
940                 DBG_8192C("bkeepfwalive(%x)\n",pwrpriv->bkeepfwalive);
941                 if(pm_netdev_open(pnetdev,_TRUE) != 0)
942                         goto error_exit;
943
944                 netif_device_attach(pnetdev);   
945                 netif_carrier_on(pnetdev);              
946                 
947 #ifdef CONFIG_AUTOSUSPEND
948                 if(pwrpriv->bInternalAutoSuspend )
949                 {                       
950                         #ifdef CONFIG_AUTOSUSPEND       
951                         #ifdef SUPPORT_HW_RFOFF_DETECTED
952                                 // The FW command register update must after MAC and FW init ready.
953                         if((padapter->bFWReady) && ( padapter->pwrctrlpriv.bHWPwrPindetect ) && (padapter->registrypriv.usbss_enable ))
954                         {
955                                 //rtl8192c_set_FwSelectSuspend_cmd(padapter,_FALSE ,500);//note fw to support hw power down ping detect
956                                 u8 bOpen = _FALSE;
957                                 rtw_interface_ps_func(padapter,HAL_USB_SELECT_SUSPEND,&bOpen);
958                         }
959                         #endif
960                         #endif
961                 
962                         pwrpriv->bInternalAutoSuspend = _FALSE;
963                         pwrpriv->brfoffbyhw = _FALSE;
964                         {
965                                 DBG_8192C("enc_algorithm(%x),wepkeymask(%x)\n",
966                                         padapter->securitypriv.dot11PrivacyAlgrthm,pwrpriv->wepkeymask);
967                                 if(     (_WEP40_ == padapter->securitypriv.dot11PrivacyAlgrthm) ||
968                                         (_WEP104_ == padapter->securitypriv.dot11PrivacyAlgrthm))
969                                 {
970                                         sint keyid;     
971                         
972                                         for(keyid=0;keyid<4;keyid++){                           
973                                                 if(pwrpriv->wepkeymask & BIT(keyid)) {
974                                                         if(keyid == padapter->securitypriv.dot11PrivacyKeyIndex)
975                                                                 rtw_set_key(padapter,&padapter->securitypriv, keyid, 1);
976                                                         else
977                                                                 rtw_set_key(padapter,&padapter->securitypriv, keyid, 0);
978                                                 }
979                                         }
980                                 }
981                         }
982                 }
983 #endif
984                 _exit_pwrlock(&pwrpriv->lock);
985         }
986         else
987         {
988                 goto error_exit;
989         }
990
991         if( padapter->pid[1]!=0) {
992                 DBG_871X("pid[1]:%d\n",padapter->pid[1]);
993                 rtw_signal_process(padapter->pid[1], SIGUSR2);
994         }       
995
996 #ifdef CONFIG_LAYER2_ROAMING_RESUME
997         rtw_roaming(padapter, NULL);
998 #endif  
999         
1000         DBG_871X("###########  rtw_resume  done#################\n");
1001         sleep_resume = 1;
1002         
1003         #ifdef CONFIG_RESUME_IN_WORKQUEUE
1004         rtw_unlock_suspend();
1005         #endif //CONFIG_RESUME_IN_WORKQUEUE
1006         
1007         _func_exit_;
1008         
1009         return 0;
1010 error_exit:
1011         DBG_8192C("%s, Open net dev failed \n",__FUNCTION__);
1012
1013         DBG_871X("###########  rtw_resume  done with error#################\n");
1014         
1015         #ifdef CONFIG_RESUME_IN_WORKQUEUE
1016         rtw_unlock_suspend();
1017         #endif //CONFIG_RESUME_IN_WORKQUEUE
1018         
1019         _func_exit_;
1020         
1021         return (-1);
1022 }
1023
1024 #ifdef CONFIG_AUTOSUSPEND
1025 void autosuspend_enter(_adapter* padapter)      
1026 {
1027         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
1028         pwrpriv->bInternalAutoSuspend = _TRUE;
1029         pwrpriv->bips_processing = _TRUE;       
1030         
1031         DBG_8192C("==>autosuspend_enter...........\n"); 
1032         
1033         if(rf_off == pwrpriv->change_rfpwrstate )
1034         {       
1035                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1036                 usb_enable_autosuspend(padapter->dvobjpriv.pusbdev);
1037                 #else
1038                 padapter->dvobjpriv.pusbdev->autosuspend_disabled = 0;//autosuspend disabled by the user        
1039                 #endif
1040         
1041                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))
1042                         usb_autopm_put_interface(padapter->dvobjpriv.pusbintf); 
1043                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))              
1044                         usb_autopm_enable(padapter->dvobjpriv.pusbintf);
1045                 #else
1046                         usb_autosuspend_device(padapter->dvobjpriv.pusbdev, 1);
1047                 #endif
1048         }
1049         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1050         DBG_8192C("...pm_usage_cnt(%d).....\n",atomic_read(&(padapter->dvobjpriv.pusbintf->pm_usage_cnt)));
1051         #else
1052         DBG_8192C("...pm_usage_cnt(%d).....\n",padapter->dvobjpriv.pusbintf->pm_usage_cnt);
1053         #endif
1054         
1055 }
1056 int autoresume_enter(_adapter* padapter)
1057 {
1058         int result = _SUCCESS;
1059         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
1060         struct security_priv* psecuritypriv=&(padapter->securitypriv);
1061         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1062         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1063         
1064         
1065         DBG_8192C("====> autoresume_enter \n");
1066         
1067         if(rf_off == pwrpriv->rf_pwrstate )
1068         {
1069                 pwrpriv->ps_flag = _FALSE;
1070                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,33))                                
1071                         if (usb_autopm_get_interface( padapter->dvobjpriv.pusbintf) < 0) 
1072                         {
1073                                 DBG_8192C( "can't get autopm: %d\n", result);
1074                                 result = _FAIL;
1075                                 goto error_exit;
1076                         }                       
1077                 #elif (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,20))                              
1078                         usb_autopm_disable(padapter->dvobjpriv.pusbintf);
1079                 #else
1080                         usb_autoresume_device(padapter->dvobjpriv.pusbdev, 1);
1081                 #endif
1082
1083                 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1084                 DBG_8192C("...pm_usage_cnt(%d).....\n",atomic_read(&(padapter->dvobjpriv.pusbintf->pm_usage_cnt)));
1085                 #else
1086                 DBG_8192C("...pm_usage_cnt(%d).....\n",padapter->dvobjpriv.pusbintf->pm_usage_cnt);
1087                 #endif  
1088         }
1089         DBG_8192C("<==== autoresume_enter \n");
1090 error_exit:     
1091
1092         return result;
1093 }
1094 #endif
1095
1096 extern char* ifname;
1097 /*
1098  * drv_init() - a device potentially for us
1099  *
1100  * notes: drv_init() is called when the bus driver has located a card for us to support.
1101  *        We accept the new device by returning 0.
1102 */
1103 static int rtw_drv_init(struct usb_interface *pusb_intf, const struct usb_device_id *pdid)
1104 {
1105         int i;
1106
1107         uint status;
1108         _adapter *padapter = NULL;
1109         struct dvobj_priv *pdvobjpriv;
1110         struct net_device *pnetdev;
1111
1112         RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("+rtw_drv_init\n"));
1113         //DBG_8192C("+rtw_drv_init\n");
1114
1115         //2009.8.13, by Thomas
1116         // In this probe function, O.S. will provide the usb interface pointer to driver.
1117         // We have to increase the reference count of the usb device structure by using the usb_get_dev function.
1118         usb_get_dev(interface_to_usbdev(pusb_intf));
1119
1120         pintf = pusb_intf;      
1121
1122         //step 0.
1123         process_spec_devid(pdid);
1124
1125         //step 1. set USB interface data
1126         // init data
1127         pnetdev = rtw_init_netdev(NULL);
1128         if (!pnetdev) 
1129                 goto error;
1130         
1131         SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
1132
1133         padapter = rtw_netdev_priv(pnetdev);
1134         padapter->bDriverStopped=_TRUE;
1135         pdvobjpriv = &padapter->dvobjpriv;
1136         pdvobjpriv->padapter = padapter;
1137         pdvobjpriv->pusbintf = pusb_intf ;
1138         pdvobjpriv->pusbdev = interface_to_usbdev(pusb_intf);
1139
1140         // set data
1141         usb_set_intfdata(pusb_intf, pnetdev);
1142
1143         //set interface_type to usb
1144         padapter->interface_type = RTW_USB;
1145
1146         //step 1-1., decide the chip_type via vid/pid
1147         decide_chip_type_by_usb_device_id(padapter, pdid);
1148
1149         //step 2.       
1150         if(padapter->chip_type == RTL8188C_8192C)
1151         {
1152 #ifdef CONFIG_RTL8192C
1153                 rtl8192cu_set_hal_ops(padapter);
1154 #endif
1155         }
1156         else if(padapter->chip_type == RTL8192D)
1157         {
1158 #ifdef CONFIG_RTL8192D
1159                 rtl8192du_set_hal_ops(padapter);
1160 #endif
1161         }
1162         else
1163         {
1164                 DBG_8192C("Detect NULL_CHIP_TYPE\n");
1165                 status = _FAIL;
1166                 goto error;
1167         }
1168
1169         //step 3.       initialize the dvobj_priv 
1170         padapter->dvobj_init=&usb_dvobj_init;
1171         padapter->dvobj_deinit=&usb_dvobj_deinit;
1172         padapter->intf_start=&usb_intf_start;
1173         padapter->intf_stop=&usb_intf_stop;
1174
1175         //step 3.
1176         //initialize the dvobj_priv ,include Chip version               
1177         if (padapter->dvobj_init == NULL){
1178                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("\n Initialize dvobjpriv.dvobj_init error!!!\n"));
1179                 goto error;
1180         }
1181
1182         status = padapter->dvobj_init(padapter);        
1183         if (status != _SUCCESS) {
1184                 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("initialize device object priv Failed!\n"));
1185                 goto error;
1186         }
1187
1188         //step 4. read efuse/eeprom data and get mac_addr
1189         intf_read_chip_info(padapter);  
1190
1191         //step 5.
1192         status = rtw_init_drv_sw(padapter);
1193         if(status ==_FAIL){
1194                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("Initialize driver software resource Failed!\n"));
1195                 goto error;
1196         }
1197
1198 #ifdef CONFIG_PM
1199 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1200         if(padapter->pwrctrlpriv.bSupportRemoteWakeup)
1201         {
1202                 pdvobjpriv->pusbdev->do_remote_wakeup=1;
1203                 pusb_intf->needs_remote_wakeup = 1;             
1204                 device_init_wakeup(&pusb_intf->dev, 1);
1205                 DBG_8192C("\n  padapter->pwrctrlpriv.bSupportRemoteWakeup~~~~~~\n");
1206                 DBG_8192C("\n  padapter->pwrctrlpriv.bSupportRemoteWakeup~~~[%d]~~~\n",device_may_wakeup(&pusb_intf->dev));
1207         }
1208 #endif
1209 #endif
1210
1211 #ifdef CONFIG_AUTOSUSPEND
1212         if( padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE )
1213         {
1214                 if(padapter->registrypriv.usbss_enable ){       /* autosuspend (2s delay) */
1215                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38))
1216                         pdvobjpriv->pusbdev->dev.power.autosuspend_delay = 0 * HZ;//15 * HZ; idle-delay time            
1217                         #else
1218                         pdvobjpriv->pusbdev->autosuspend_delay = 0 * HZ;//15 * HZ; idle-delay time                      
1219                         #endif
1220
1221                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1222                         usb_enable_autosuspend(padapter->dvobjpriv.pusbdev);
1223                         #elif  (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22) && LINUX_VERSION_CODE<=KERNEL_VERSION(2,6,34))
1224                         padapter->bDisableAutosuspend = padapter->dvobjpriv.pusbdev->autosuspend_disabled ;
1225                         padapter->dvobjpriv.pusbdev->autosuspend_disabled = 0;//autosuspend disabled by the user
1226                         #endif
1227
1228                         usb_autopm_get_interface(padapter->dvobjpriv.pusbintf );//init pm_usage_cnt ,let it start from 1
1229
1230                         #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,32))
1231                         DBG_8192C("%s...pm_usage_cnt(%d).....\n",__FUNCTION__,atomic_read(&(pdvobjpriv->pusbintf ->pm_usage_cnt)));
1232                         #else
1233                         DBG_8192C("%s...pm_usage_cnt(%d).....\n",__FUNCTION__,pdvobjpriv->pusbintf ->pm_usage_cnt);
1234                         #endif                                                  
1235                 }
1236         }       
1237 #endif
1238         // alloc dev name after read efuse.
1239         rtw_init_netdev_name(pnetdev, ifname);
1240
1241         rtw_macaddr_cfg(padapter->eeprompriv.mac_addr);
1242
1243         _rtw_memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
1244         DBG_8192C("MAC Address from pnetdev->dev_addr= " MAC_FMT "\n", MAC_ARG(pnetdev->dev_addr));     
1245
1246
1247         //step 6.
1248         /* Tell the network stack we exist */
1249         if (register_netdev(pnetdev) != 0) {
1250                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("register_netdev() failed\n"));
1251                 goto error;
1252         }
1253
1254         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-drv_init - Adapter->bDriverStopped=%d, Adapter->bSurpriseRemoved=%d\n",padapter->bDriverStopped, padapter->bSurpriseRemoved));
1255         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-871x_drv - drv_init, success!\n"));
1256         //DBG_8192C("-871x_drv - drv_init, success!\n");
1257
1258 #ifdef CONFIG_PROC_DEBUG
1259 #ifdef RTK_DMP_PLATFORM
1260         rtw_proc_init_one(pnetdev);
1261 #endif
1262 #endif
1263
1264 #ifdef CONFIG_HOSTAPD_MLME
1265         hostapd_mode_init(padapter);
1266 #endif
1267
1268 #ifdef CONFIG_PLATFORM_RTD2880B
1269         DBG_8192C("wlan link up\n");
1270         rtd2885_wlan_netlink_sendMsg("linkup", "8712");
1271 #endif
1272
1273
1274 #ifdef CONFIG_GLOBAL_UI_PID
1275         if(ui_pid[1]!=0) {
1276                 DBG_871X("ui_pid[1]:%d\n",ui_pid[1]);
1277                 rtw_signal_process(ui_pid[1], SIGUSR2);
1278         }
1279 #endif
1280
1281         DBG_8192C("bDriverStopped:%d, bSurpriseRemoved:%d, bup:%d, hw_init_completed:%d\n"
1282                 ,padapter->bDriverStopped
1283                 ,padapter->bSurpriseRemoved
1284                 ,padapter->bup
1285                 ,padapter->hw_init_completed
1286         );
1287         rtl8188_power_save_init();
1288
1289         return 0;
1290
1291 error:
1292
1293         usb_put_dev(interface_to_usbdev(pusb_intf));//decrease the reference count of the usb device structure if driver fail on initialzation
1294
1295         usb_set_intfdata(pusb_intf, NULL);
1296
1297         usb_dvobj_deinit(padapter);
1298         
1299         if (pnetdev)
1300         {
1301                 //unregister_netdev(pnetdev);
1302                 rtw_free_netdev(pnetdev);
1303         }
1304
1305         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-871x_usb - drv_init, fail!\n"));
1306         //DBG_8192C("-871x_usb - drv_init, fail!\n");
1307
1308         return -ENODEV;
1309 }
1310
1311 /*
1312  * dev_remove() - our device is being removed
1313 */
1314 //rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove() => how to recognize both
1315 static void rtw_dev_remove(struct usb_interface *pusb_intf)
1316 {
1317         struct net_device *pnetdev=usb_get_intfdata(pusb_intf);
1318         _adapter *padapter = (_adapter*)rtw_netdev_priv(pnetdev);
1319         struct mlme_priv *pmlmepriv= &padapter->mlmepriv;
1320         struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
1321         u8      bResetDevice = _FALSE;
1322
1323 _func_exit_;
1324
1325         usb_set_intfdata(pusb_intf, NULL);
1326
1327         if(padapter)
1328         {
1329                 DBG_8192C("+rtw_dev_remove\n");
1330                 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+dev_remove()\n"));
1331
1332                 LeaveAllPowerSaveMode(padapter);
1333
1334                 if(check_fwstate(pmlmepriv, _FW_LINKED))
1335                         disconnect_hdl(padapter, NULL);
1336
1337                 if(drvpriv.drv_registered == _TRUE)
1338                 {
1339                         //DBG_8192C("r871xu_dev_remove():padapter->bSurpriseRemoved == _TRUE\n");
1340                         padapter->bSurpriseRemoved = _TRUE;
1341                 }
1342                 /*else
1343                 {
1344                         //DBG_8192C("r871xu_dev_remove():module removed\n");
1345                         padapter->hw_init_completed = _FALSE;
1346                 }*/
1347
1348 #ifdef CONFIG_AP_MODE
1349                 free_mlme_ap_info(padapter);
1350 #ifdef CONFIG_HOSTAPD_MLME
1351                 hostapd_mode_unload(padapter);
1352 #endif //CONFIG_HOSTAPD_MLME
1353 #endif //CONFIG_AP_MODE
1354
1355                 if(padapter->DriverState != DRIVER_DISAPPEAR)
1356                 {
1357                         if(pnetdev) {
1358                                 unregister_netdev(pnetdev); //will call netdev_close()
1359 #ifdef CONFIG_PROC_DEBUG
1360                                 rtw_proc_remove_one(pnetdev);
1361 #endif
1362                         }
1363                 }
1364
1365                 rtw_cancel_all_timer(padapter);
1366
1367                 rtw_dev_unload(padapter);
1368
1369                 DBG_8192C("+r871xu_dev_remove, hw_init_completed=%d\n", padapter->hw_init_completed);
1370
1371                 //Modify condition for 92DU DMDP 2010.11.18, by Thomas
1372                 //move code to here, avoid access null pointer. 2011.05.25.
1373                 if((pdvobjpriv->NumInterfaces != 2) || (pdvobjpriv->InterfaceNumber == 1))
1374                         bResetDevice = _TRUE;
1375
1376                 //s6.
1377                 if(padapter->dvobj_deinit)
1378                 {
1379                         padapter->dvobj_deinit(padapter);
1380                 }
1381                 else
1382                 {
1383                         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("Initialize hcipriv.hci_priv_init error!!!\n"));
1384                 }
1385
1386                 //after rtw_free_drv_sw(), padapter has beed freed, don't refer to it.
1387                 rtw_free_drv_sw(padapter);      
1388                 
1389         }
1390
1391         usb_put_dev(interface_to_usbdev(pusb_intf));//decrease the reference count of the usb device structure when disconnect
1392
1393         //If we didn't unplug usb dongle and remove/insert modlue, driver fails on sitesurvey for the first time when device is up . 
1394         //Reset usb port for sitesurvey fail issue. 2009.8.13, by Thomas
1395         if(_TRUE == bResetDevice)
1396         {
1397                 if(interface_to_usbdev(pusb_intf)->state != USB_STATE_NOTATTACHED)
1398                 {
1399                         DBG_8192C("usb attached..., try to reset usb device\n");
1400                         usb_reset_device(interface_to_usbdev(pusb_intf));
1401                 }
1402         }
1403         
1404         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-dev_remove()\n"));
1405         DBG_8192C("-r871xu_dev_remove, done\n");
1406
1407 #ifdef CONFIG_PLATFORM_RTD2880B
1408         DBG_8192C("wlan link down\n");
1409         rtd2885_wlan_netlink_sendMsg("linkdown", "8712");
1410 #endif
1411
1412         #ifdef DBG_MEM_ALLOC
1413         rtw_dump_mem_stat ();
1414         #endif
1415 _func_exit_;
1416         rtl8188_power_save_exit();
1417
1418         return;
1419
1420 }
1421 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)) 
1422 extern int console_suspend_enabled;
1423 #endif
1424
1425 static int rtw_drv_entry(void)
1426 {
1427 #ifdef CONFIG_PLATFORM_RTK_DMP
1428         u32 tmp;
1429         tmp=readl((volatile unsigned int*)0xb801a608);
1430         tmp &= 0xffffff00;
1431         tmp |= 0x55;
1432         writel(tmp,(volatile unsigned int*)0xb801a608);//write dummy register for 1055
1433 #endif
1434
1435
1436         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+rtw_drv_entry\n"));
1437
1438         DBG_8192C("\nrtw driver version=%s\n", DRIVERVERSION);          
1439 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)) 
1440         //console_suspend_enabled=0;
1441 #endif  
1442
1443         rtw_suspend_lock_init();
1444
1445         drvpriv.drv_registered = _TRUE;
1446         return usb_register(&drvpriv.rtw_usb_drv);
1447 }
1448
1449 static void rtw_drv_halt(void)
1450 {
1451         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("+rtw_drv_halt\n"));
1452         DBG_8192C("+rtw_drv_halt\n");
1453
1454         rtw_suspend_lock_uninit();
1455
1456         drvpriv.drv_registered = _FALSE;
1457         usb_deregister(&drvpriv.rtw_usb_drv);
1458         DBG_8192C("-rtw_drv_halt\n");
1459 }
1460
1461
1462 //module_init(rtw_drv_entry);
1463 //module_exit(rtw_drv_halt);
1464
1465 #include "wifi_version.h"
1466
1467 int mv88w8686_if_sdio_init_module(void)
1468 {
1469         printk("RTL8192/RTL8188 USB WiFi driver (Powered by Rockchip,Ver %s) init.\n", RTL8192_DRV_VERSION);
1470
1471     wifi_usb_init();
1472     wifi_activate_usb();
1473
1474     return rtw_drv_entry();
1475 }
1476
1477 void  mv88w8686_if_sdio_exit_module(void)
1478 {
1479         rtw_drv_halt();
1480         wifi_deactivate_usb();
1481 }
1482
1483 EXPORT_SYMBOL(mv88w8686_if_sdio_init_module);
1484 EXPORT_SYMBOL(mv88w8686_if_sdio_exit_module);
1485
1486 int rockchip_wifi_init_module(void)
1487 {
1488         return mv88w8686_if_sdio_init_module();
1489 }
1490
1491 void rockchip_wifi_exit_module(void)
1492 {
1493         mv88w8686_if_sdio_exit_module();
1494 }
1495
1496 EXPORT_SYMBOL(rockchip_wifi_init_module);
1497 EXPORT_SYMBOL(rockchip_wifi_exit_module);