ddacfc6c486179274394b330153c901dfc7c63b7
[pandora-kernel.git] / drivers / staging / rt2860 / usb_main_dev.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************/
26
27 #include "rt_config.h"
28
29 /* Following information will be show when you run 'modinfo' */
30 /* *** If you have a solution for the bug in current version of driver, please mail to me. */
31 /* Otherwise post to forum in ralinktech's web site(www.ralinktech.com) and let all users help you. *** */
32 MODULE_AUTHOR("Paul Lin <paul_lin@ralinktech.com>");
33 MODULE_DESCRIPTION("RT2870/RT3070 Wireless Lan Linux Driver");
34 MODULE_LICENSE("GPL");
35 #ifdef MODULE_VERSION
36 MODULE_VERSION(STA_DRIVER_VERSION);
37 #endif
38
39 /* module table */
40 struct usb_device_id rtusb_usb_id[] = {
41 #ifdef RT2870
42         {USB_DEVICE(0x148F, 0x2770)},   /* Ralink */
43         {USB_DEVICE(0x148F, 0x2870)},   /* Ralink */
44         {USB_DEVICE(0x07B8, 0x2870)},   /* AboCom */
45         {USB_DEVICE(0x07B8, 0x2770)},   /* AboCom */
46         {USB_DEVICE(0x0DF6, 0x0039)},   /* Sitecom 2770 */
47         {USB_DEVICE(0x0DF6, 0x003F)},   /* Sitecom 2770 */
48         {USB_DEVICE(0x083A, 0x7512)},   /* Arcadyan 2770 */
49         {USB_DEVICE(0x0789, 0x0162)},   /* Logitec 2870 */
50         {USB_DEVICE(0x0789, 0x0163)},   /* Logitec 2870 */
51         {USB_DEVICE(0x0789, 0x0164)},   /* Logitec 2870 */
52         {USB_DEVICE(0x177f, 0x0302)},   /* lsusb */
53         {USB_DEVICE(0x0B05, 0x1731)},   /* Asus */
54         {USB_DEVICE(0x0B05, 0x1732)},   /* Asus */
55         {USB_DEVICE(0x0B05, 0x1742)},   /* Asus */
56         {USB_DEVICE(0x0DF6, 0x0017)},   /* Sitecom */
57         {USB_DEVICE(0x0DF6, 0x002B)},   /* Sitecom */
58         {USB_DEVICE(0x0DF6, 0x002C)},   /* Sitecom */
59         {USB_DEVICE(0x0DF6, 0x002D)},   /* Sitecom */
60         {USB_DEVICE(0x14B2, 0x3C06)},   /* Conceptronic */
61         {USB_DEVICE(0x14B2, 0x3C28)},   /* Conceptronic */
62         {USB_DEVICE(0x2019, 0xED06)},   /* Planex Communications, Inc. */
63         {USB_DEVICE(0x07D1, 0x3C09)},   /* D-Link */
64         {USB_DEVICE(0x07D1, 0x3C11)},   /* D-Link */
65         {USB_DEVICE(0x14B2, 0x3C07)},   /* AL */
66         {USB_DEVICE(0x050D, 0x8053)},   /* Belkin */
67         {USB_DEVICE(0x050D, 0x825B)},   /* Belkin */
68         {USB_DEVICE(0x050D, 0x935A)},   /* Belkin F6D4050 v1 */
69         {USB_DEVICE(0x050D, 0x935B)},   /* Belkin F6D4050 v2 */
70         {USB_DEVICE(0x14B2, 0x3C23)},   /* Airlink */
71         {USB_DEVICE(0x14B2, 0x3C27)},   /* Airlink */
72         {USB_DEVICE(0x07AA, 0x002F)},   /* Corega */
73         {USB_DEVICE(0x07AA, 0x003C)},   /* Corega */
74         {USB_DEVICE(0x07AA, 0x003F)},   /* Corega */
75         {USB_DEVICE(0x1044, 0x800B)},   /* Gigabyte */
76         {USB_DEVICE(0x15A9, 0x0006)},   /* Sparklan */
77         {USB_DEVICE(0x083A, 0xB522)},   /* SMC */
78         {USB_DEVICE(0x083A, 0xA618)},   /* SMC */
79         {USB_DEVICE(0x083A, 0x8522)},   /* Arcadyan */
80         {USB_DEVICE(0x083A, 0x7522)},   /* Arcadyan */
81         {USB_DEVICE(0x0CDE, 0x0022)},   /* ZCOM */
82         {USB_DEVICE(0x0586, 0x3416)},   /* Zyxel */
83         {USB_DEVICE(0x0586, 0x341a)},   /* Zyxel NWD-270N */
84         {USB_DEVICE(0x0CDE, 0x0025)},   /* Zyxel */
85         {USB_DEVICE(0x1740, 0x9701)},   /* EnGenius */
86         {USB_DEVICE(0x1740, 0x9702)},   /* EnGenius */
87         {USB_DEVICE(0x0471, 0x200f)},   /* Philips */
88         {USB_DEVICE(0x14B2, 0x3C25)},   /* Draytek */
89         {USB_DEVICE(0x13D3, 0x3247)},   /* AzureWave */
90         {USB_DEVICE(0x083A, 0x6618)},   /* Accton */
91         {USB_DEVICE(0x15c5, 0x0008)},   /* Amit */
92         {USB_DEVICE(0x0E66, 0x0001)},   /* Hawking */
93         {USB_DEVICE(0x0E66, 0x0003)},   /* Hawking */
94         {USB_DEVICE(0x129B, 0x1828)},   /* Siemens */
95         {USB_DEVICE(0x157E, 0x300E)},   /* U-Media */
96         {USB_DEVICE(0x050d, 0x805c)},
97         {USB_DEVICE(0x050d, 0x815c)},
98         {USB_DEVICE(0x1482, 0x3C09)},   /* Abocom */
99         {USB_DEVICE(0x14B2, 0x3C09)},   /* Alpha */
100         {USB_DEVICE(0x04E8, 0x2018)},   /* samsung linkstick2 */
101         {USB_DEVICE(0x1690, 0x0740)},   /* Askey */
102         {USB_DEVICE(0x5A57, 0x0280)},   /* Zinwell */
103         {USB_DEVICE(0x5A57, 0x0282)},   /* Zinwell */
104         {USB_DEVICE(0x7392, 0x7718)},
105         {USB_DEVICE(0x7392, 0x7717)},
106         {USB_DEVICE(0x0411, 0x016f)},   /* MelCo.,Inc. WLI-UC-G301N */
107         {USB_DEVICE(0x1737, 0x0070)},   /* Linksys WUSB100 */
108         {USB_DEVICE(0x1737, 0x0071)},   /* Linksys WUSB600N */
109         {USB_DEVICE(0x0411, 0x00e8)},   /* Buffalo WLI-UC-G300N */
110         {USB_DEVICE(0x050d, 0x815c)},   /* Belkin F5D8053 */
111         {USB_DEVICE(0x100D, 0x9031)},   /* Motorola 2770 */
112 #endif /* RT2870 // */
113 #ifdef RT3070
114         {USB_DEVICE(0x148F, 0x3070)},   /* Ralink 3070 */
115         {USB_DEVICE(0x148F, 0x3071)},   /* Ralink 3071 */
116         {USB_DEVICE(0x148F, 0x3072)},   /* Ralink 3072 */
117         {USB_DEVICE(0x0DB0, 0x3820)},   /* Ralink 3070 */
118         {USB_DEVICE(0x0DB0, 0x871C)},   /* Ralink 3070 */
119         {USB_DEVICE(0x0DB0, 0x822C)},   /* Ralink 3070 */
120         {USB_DEVICE(0x0DB0, 0x871B)},   /* Ralink 3070 */
121         {USB_DEVICE(0x0DB0, 0x822B)},   /* Ralink 3070 */
122         {USB_DEVICE(0x0DF6, 0x003E)},   /* Sitecom 3070 */
123         {USB_DEVICE(0x0DF6, 0x0042)},   /* Sitecom 3072 */
124         {USB_DEVICE(0x0DF6, 0x0048)},   /* Sitecom 3070 */
125         {USB_DEVICE(0x0DF6, 0x0047)},   /* Sitecom 3071 */
126         {USB_DEVICE(0x14B2, 0x3C12)},   /* AL 3070 */
127         {USB_DEVICE(0x18C5, 0x0012)},   /* Corega 3070 */
128         {USB_DEVICE(0x083A, 0x7511)},   /* Arcadyan 3070 */
129         {USB_DEVICE(0x083A, 0xA701)},   /* SMC 3070 */
130         {USB_DEVICE(0x083A, 0xA702)},   /* SMC 3072 */
131         {USB_DEVICE(0x1740, 0x9703)},   /* EnGenius 3070 */
132         {USB_DEVICE(0x1740, 0x9705)},   /* EnGenius 3071 */
133         {USB_DEVICE(0x1740, 0x9706)},   /* EnGenius 3072 */
134         {USB_DEVICE(0x1740, 0x9707)},   /* EnGenius 3070 */
135         {USB_DEVICE(0x1740, 0x9708)},   /* EnGenius 3071 */
136         {USB_DEVICE(0x1740, 0x9709)},   /* EnGenius 3072 */
137         {USB_DEVICE(0x13D3, 0x3273)},   /* AzureWave 3070 */
138         {USB_DEVICE(0x13D3, 0x3305)},   /* AzureWave 3070*/
139         {USB_DEVICE(0x1044, 0x800D)},   /* Gigabyte GN-WB32L 3070 */
140         {USB_DEVICE(0x2019, 0xAB25)},   /* Planex Communications, Inc. RT3070 */
141         {USB_DEVICE(0x07B8, 0x3070)},   /* AboCom 3070 */
142         {USB_DEVICE(0x07B8, 0x3071)},   /* AboCom 3071 */
143         {USB_DEVICE(0x07B8, 0x3072)},   /* Abocom 3072 */
144         {USB_DEVICE(0x7392, 0x7711)},   /* Edimax 3070 */
145         {USB_DEVICE(0x1A32, 0x0304)},   /* Quanta 3070 */
146         {USB_DEVICE(0x1EDA, 0x2310)},   /* AirTies 3070 */
147         {USB_DEVICE(0x07D1, 0x3C0A)},   /* D-Link 3072 */
148         {USB_DEVICE(0x07D1, 0x3C0D)},   /* D-Link 3070 */
149         {USB_DEVICE(0x07D1, 0x3C0E)},   /* D-Link 3070 */
150         {USB_DEVICE(0x07D1, 0x3C0F)},   /* D-Link 3070 */
151         {USB_DEVICE(0x07D1, 0x3C16)},   /* D-Link 3070 */
152         {USB_DEVICE(0x07D1, 0x3C17)},   /* D-Link 8070 */
153         {USB_DEVICE(0x1D4D, 0x000C)},   /* Pegatron Corporation 3070 */
154         {USB_DEVICE(0x1D4D, 0x000E)},   /* Pegatron Corporation 3070 */
155         {USB_DEVICE(0x5A57, 0x5257)},   /* Zinwell 3070 */
156         {USB_DEVICE(0x5A57, 0x0283)},   /* Zinwell 3072 */
157         {USB_DEVICE(0x04BB, 0x0945)},   /* I-O DATA 3072 */
158         {USB_DEVICE(0x04BB, 0x0947)},   /* I-O DATA 3070 */
159         {USB_DEVICE(0x04BB, 0x0948)},   /* I-O DATA 3072 */
160         {USB_DEVICE(0x203D, 0x1480)},   /* Encore 3070 */
161         {USB_DEVICE(0x20B8, 0x8888)},   /* PARA INDUSTRIAL 3070 */
162         {USB_DEVICE(0x0B05, 0x1784)},   /* Asus 3072 */
163         {USB_DEVICE(0x203D, 0x14A9)},   /* Encore 3070*/
164         {USB_DEVICE(0x0DB0, 0x899A)},   /* MSI 3070*/
165         {USB_DEVICE(0x0DB0, 0x3870)},   /* MSI 3070*/
166         {USB_DEVICE(0x0DB0, 0x870A)},   /* MSI 3070*/
167         {USB_DEVICE(0x0DB0, 0x6899)},   /* MSI 3070 */
168         {USB_DEVICE(0x0DB0, 0x3822)},   /* MSI 3070 */
169         {USB_DEVICE(0x0DB0, 0x3871)},   /* MSI 3070 */
170         {USB_DEVICE(0x0DB0, 0x871A)},   /* MSI 3070 */
171         {USB_DEVICE(0x0DB0, 0x822A)},   /* MSI 3070 */
172         {USB_DEVICE(0x0DB0, 0x3821)},   /* Ralink 3070 */
173         {USB_DEVICE(0x0DB0, 0x821A)},   /* Ralink 3070 */
174         {USB_DEVICE(0x083A, 0xA703)},   /* IO-MAGIC */
175         {USB_DEVICE(0x13D3, 0x3307)},   /* Azurewave */
176         {USB_DEVICE(0x13D3, 0x3321)},   /* Azurewave */
177         {USB_DEVICE(0x07FA, 0x7712)},   /* Edimax */
178         {USB_DEVICE(0x0789, 0x0166)},   /* Edimax */
179         {USB_DEVICE(0x148F, 0x2070)},   /* Edimax */
180 #endif /* RT3070 // */
181         {USB_DEVICE(0x1737, 0x0077)},   /* Linksys WUSB54GC-EU v3 */
182         {USB_DEVICE(0x2001, 0x3C09)},   /* D-Link */
183         {USB_DEVICE(0x2001, 0x3C0A)},   /* D-Link 3072 */
184         {USB_DEVICE(0x2019, 0xED14)},   /* Planex Communications, Inc. */
185         {}                      /* Terminating entry */
186 };
187
188 int const rtusb_usb_id_len =
189     sizeof(rtusb_usb_id) / sizeof(struct usb_device_id);
190
191 MODULE_DEVICE_TABLE(usb, rtusb_usb_id);
192
193 static void rt2870_disconnect(struct usb_device *dev, struct rt_rtmp_adapter *pAd);
194
195 static int __devinit rt2870_probe(IN struct usb_interface *intf,
196                                   IN struct usb_device *usb_dev,
197                                   IN const struct usb_device_id *dev_id,
198                                   struct rt_rtmp_adapter **ppAd);
199
200 #ifndef PF_NOFREEZE
201 #define PF_NOFREEZE  0
202 #endif
203
204 extern int rt28xx_close(IN struct net_device *net_dev);
205 extern int rt28xx_open(struct net_device *net_dev);
206
207 static BOOLEAN USBDevConfigInit(IN struct usb_device *dev,
208                                 IN struct usb_interface *intf,
209                                 struct rt_rtmp_adapter *pAd);
210
211 /*
212 ========================================================================
213 Routine Description:
214     Check the chipset vendor/product ID.
215
216 Arguments:
217     _dev_p                              Point to the PCI or USB device
218
219 Return Value:
220     TRUE                                Check ok
221         FALSE                           Check fail
222
223 Note:
224 ========================================================================
225 */
226 BOOLEAN RT28XXChipsetCheck(IN void *_dev_p)
227 {
228         struct usb_interface *intf = (struct usb_interface *)_dev_p;
229         struct usb_device *dev_p = interface_to_usbdev(intf);
230         u32 i;
231
232         for (i = 0; i < rtusb_usb_id_len; i++) {
233                 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
234                     dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct) {
235                         printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
236                                dev_p->descriptor.idVendor,
237                                dev_p->descriptor.idProduct);
238                         break;
239                 }
240         }
241
242         if (i == rtusb_usb_id_len) {
243                 printk("rt2870: Error! Device Descriptor not matching!\n");
244                 return FALSE;
245         }
246
247         return TRUE;
248 }
249
250 /**************************************************************************/
251 /**************************************************************************/
252 /*tested for kernel 2.6series */
253 /**************************************************************************/
254 /**************************************************************************/
255
256 #ifdef CONFIG_PM
257 static int rt2870_suspend(struct usb_interface *intf, pm_message_t state);
258 static int rt2870_resume(struct usb_interface *intf);
259 #endif /* CONFIG_PM // */
260
261 static BOOLEAN USBDevConfigInit(IN struct usb_device *dev,
262                                 IN struct usb_interface *intf,
263                                 struct rt_rtmp_adapter *pAd)
264 {
265         struct usb_host_interface *iface_desc;
266         unsigned long BulkOutIdx;
267         u32 i;
268
269         /* get the active interface descriptor */
270         iface_desc = intf->cur_altsetting;
271
272         /* get # of enpoints  */
273         pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
274         DBGPRINT(RT_DEBUG_TRACE,
275                  ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
276
277         /* Configure Pipes */
278         BulkOutIdx = 0;
279
280         for (i = 0; i < pAd->NumberOfPipes; i++) {
281                 if ((iface_desc->endpoint[i].desc.bmAttributes ==
282                      USB_ENDPOINT_XFER_BULK) &&
283                     ((iface_desc->endpoint[i].desc.bEndpointAddress &
284                       USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)) {
285                         pAd->BulkInEpAddr =
286                             iface_desc->endpoint[i].desc.bEndpointAddress;
287                         pAd->BulkInMaxPacketSize =
288                             le2cpu16(iface_desc->endpoint[i].desc.
289                                      wMaxPacketSize);
290
291                         DBGPRINT_RAW(RT_DEBUG_TRACE,
292                                      ("BULK IN MaxPacketSize = %d\n",
293                                       pAd->BulkInMaxPacketSize));
294                         DBGPRINT_RAW(RT_DEBUG_TRACE,
295                                      ("EP address = 0x%2x\n",
296                                       iface_desc->endpoint[i].desc.
297                                       bEndpointAddress));
298                 } else
299                     if ((iface_desc->endpoint[i].desc.bmAttributes ==
300                          USB_ENDPOINT_XFER_BULK)
301                         &&
302                         ((iface_desc->endpoint[i].desc.
303                           bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
304                          USB_DIR_OUT)) {
305                         /* there are 6 bulk out EP. EP6 highest priority. */
306                         /* EP1-4 is EDCA.  EP5 is HCCA. */
307                         pAd->BulkOutEpAddr[BulkOutIdx++] =
308                             iface_desc->endpoint[i].desc.bEndpointAddress;
309                         pAd->BulkOutMaxPacketSize =
310                             le2cpu16(iface_desc->endpoint[i].desc.
311                                      wMaxPacketSize);
312
313                         DBGPRINT_RAW(RT_DEBUG_TRACE,
314                                      ("BULK OUT MaxPacketSize = %d\n",
315                                       pAd->BulkOutMaxPacketSize));
316                         DBGPRINT_RAW(RT_DEBUG_TRACE,
317                                      ("EP address = 0x%2x  \n",
318                                       iface_desc->endpoint[i].desc.
319                                       bEndpointAddress));
320                 }
321         }
322
323         if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0])) {
324                 printk
325                     ("%s: Could not find both bulk-in and bulk-out endpoints\n",
326                      __FUNCTION__);
327                 return FALSE;
328         }
329
330         pAd->config = &dev->config->desc;
331         usb_set_intfdata(intf, pAd);
332
333         return TRUE;
334
335 }
336
337 static int __devinit rtusb_probe(struct usb_interface *intf,
338                        const struct usb_device_id *id)
339 {
340         struct rt_rtmp_adapter *pAd;
341         struct usb_device *dev;
342         int rv;
343
344         dev = interface_to_usbdev(intf);
345         dev = usb_get_dev(dev);
346
347         rv = rt2870_probe(intf, dev, id, &pAd);
348         if (rv != 0)
349                 usb_put_dev(dev);
350
351         return rv;
352 }
353
354 static void rtusb_disconnect(struct usb_interface *intf)
355 {
356         struct usb_device *dev = interface_to_usbdev(intf);
357         struct rt_rtmp_adapter *pAd;
358
359         pAd = usb_get_intfdata(intf);
360         usb_set_intfdata(intf, NULL);
361
362         rt2870_disconnect(dev, pAd);
363 }
364
365 struct usb_driver rtusb_driver = {
366         .name = "rt2870",
367         .probe = rtusb_probe,
368         .disconnect = rtusb_disconnect,
369         .id_table = rtusb_usb_id,
370
371 #ifdef CONFIG_PM
372 suspend:rt2870_suspend,
373 resume:rt2870_resume,
374 #endif
375 };
376
377 #ifdef CONFIG_PM
378
379 void RT2870RejectPendingPackets(struct rt_rtmp_adapter *pAd)
380 {
381         /* clear PS packets */
382         /* clear TxSw packets */
383 }
384
385 static int rt2870_suspend(struct usb_interface *intf, pm_message_t state)
386 {
387         struct net_device *net_dev;
388         struct rt_rtmp_adapter *pAd = usb_get_intfdata(intf);
389
390         DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_suspend()\n"));
391         net_dev = pAd->net_dev;
392         netif_device_detach(net_dev);
393
394         pAd->PM_FlgSuspend = 1;
395         if (netif_running(net_dev)) {
396                 RTUSBCancelPendingBulkInIRP(pAd);
397                 RTUSBCancelPendingBulkOutIRP(pAd);
398         }
399         DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_suspend()\n"));
400         return 0;
401 }
402
403 static int rt2870_resume(struct usb_interface *intf)
404 {
405         struct net_device *net_dev;
406         struct rt_rtmp_adapter *pAd = usb_get_intfdata(intf);
407
408         DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_resume()\n"));
409
410         pAd->PM_FlgSuspend = 0;
411         net_dev = pAd->net_dev;
412         netif_device_attach(net_dev);
413         netif_start_queue(net_dev);
414         netif_carrier_on(net_dev);
415         netif_wake_queue(net_dev);
416
417         DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_resume()\n"));
418         return 0;
419 }
420 #endif /* CONFIG_PM // */
421
422 /* Init driver module */
423 int __init rtusb_init(void)
424 {
425         printk("rtusb init --->\n");
426         return usb_register(&rtusb_driver);
427 }
428
429 /* Deinit driver module */
430 void __exit rtusb_exit(void)
431 {
432         usb_deregister(&rtusb_driver);
433         printk("<--- rtusb exit\n");
434 }
435
436 module_init(rtusb_init);
437 module_exit(rtusb_exit);
438
439 /*--------------------------------------------------------------------- */
440 /* function declarations                                                                                                */
441 /*--------------------------------------------------------------------- */
442
443 /*
444 ========================================================================
445 Routine Description:
446     MLME kernel thread.
447
448 Arguments:
449         *Context                        the pAd, driver control block pointer
450
451 Return Value:
452     0                                   close the thread
453
454 Note:
455 ========================================================================
456 */
457 int MlmeThread(IN void *Context)
458 {
459         struct rt_rtmp_adapter *pAd;
460         struct rt_rtmp_os_task *pTask;
461         int status;
462         status = 0;
463
464         pTask = Context;
465         pAd = pTask->priv;
466
467         RtmpOSTaskCustomize(pTask);
468
469         while (!pTask->task_killed) {
470 #ifdef KTHREAD_SUPPORT
471                 RTMP_WAIT_EVENT_INTERRUPTIBLE(pAd, pTask);
472 #else
473                 RTMP_SEM_EVENT_WAIT(&(pTask->taskSema), status);
474
475                 /* unlock the device pointers */
476                 if (status != 0) {
477                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
478                         break;
479                 }
480 #endif
481
482                 /* lock the device pointers , need to check if required */
483                 /*down(&(pAd->usbdev_semaphore)); */
484
485                 if (!pAd->PM_FlgSuspend)
486                         MlmeHandler(pAd);
487         }
488
489         /* notify the exit routine that we're actually exiting now
490          *
491          * complete()/wait_for_completion() is similar to up()/down(),
492          * except that complete() is safe in the case where the structure
493          * is getting deleted in a parallel mode of execution (i.e. just
494          * after the down() -- that's necessary for the thread-shutdown
495          * case.
496          *
497          * complete_and_exit() goes even further than this -- it is safe in
498          * the case that the thread of the caller is going away (not just
499          * the structure) -- this is necessary for the module-remove case.
500          * This is important in preemption kernels, which transfer the flow
501          * of execution immediately upon a complete().
502          */
503         DBGPRINT(RT_DEBUG_TRACE, ("<---%s\n", __FUNCTION__));
504 #ifndef KTHREAD_SUPPORT
505         pTask->taskPID = THREAD_PID_INIT_VALUE;
506         complete_and_exit(&pTask->taskComplete, 0);
507 #endif
508         return 0;
509
510 }
511
512 /*
513 ========================================================================
514 Routine Description:
515     USB command kernel thread.
516
517 Arguments:
518         *Context                        the pAd, driver control block pointer
519
520 Return Value:
521     0                                   close the thread
522
523 Note:
524 ========================================================================
525 */
526 int RTUSBCmdThread(IN void *Context)
527 {
528         struct rt_rtmp_adapter *pAd;
529         struct rt_rtmp_os_task *pTask;
530         int status;
531         status = 0;
532
533         pTask = Context;
534         pAd = pTask->priv;
535
536         RtmpOSTaskCustomize(pTask);
537
538         NdisAcquireSpinLock(&pAd->CmdQLock);
539         pAd->CmdQ.CmdQState = RTMP_TASK_STAT_RUNNING;
540         NdisReleaseSpinLock(&pAd->CmdQLock);
541
542         while (pAd && pAd->CmdQ.CmdQState == RTMP_TASK_STAT_RUNNING) {
543 #ifdef KTHREAD_SUPPORT
544                 RTMP_WAIT_EVENT_INTERRUPTIBLE(pAd, pTask);
545 #else
546                 /* lock the device pointers */
547                 RTMP_SEM_EVENT_WAIT(&(pTask->taskSema), status);
548
549                 if (status != 0) {
550                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
551                         break;
552                 }
553 #endif
554
555                 if (pAd->CmdQ.CmdQState == RTMP_TASK_STAT_STOPED)
556                         break;
557
558                 if (!pAd->PM_FlgSuspend)
559                         CMDHandler(pAd);
560         }
561
562         if (pAd && !pAd->PM_FlgSuspend) {       /* Clear the CmdQElements. */
563                 struct rt_cmdqelmt *pCmdQElmt = NULL;
564
565                 NdisAcquireSpinLock(&pAd->CmdQLock);
566                 pAd->CmdQ.CmdQState = RTMP_TASK_STAT_STOPED;
567                 while (pAd->CmdQ.size) {
568                         RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
569                         if (pCmdQElmt) {
570                                 if (pCmdQElmt->CmdFromNdis == TRUE) {
571                                         if (pCmdQElmt->buffer != NULL)
572                                                 os_free_mem(pAd,
573                                                             pCmdQElmt->buffer);
574                                         os_free_mem(pAd, (u8 *)pCmdQElmt);
575                                 } else {
576                                         if ((pCmdQElmt->buffer != NULL)
577                                             && (pCmdQElmt->bufferlength != 0))
578                                                 os_free_mem(pAd,
579                                                             pCmdQElmt->buffer);
580                                         os_free_mem(pAd, (u8 *)pCmdQElmt);
581                                 }
582                         }
583                 }
584
585                 NdisReleaseSpinLock(&pAd->CmdQLock);
586         }
587         /* notify the exit routine that we're actually exiting now
588          *
589          * complete()/wait_for_completion() is similar to up()/down(),
590          * except that complete() is safe in the case where the structure
591          * is getting deleted in a parallel mode of execution (i.e. just
592          * after the down() -- that's necessary for the thread-shutdown
593          * case.
594          *
595          * complete_and_exit() goes even further than this -- it is safe in
596          * the case that the thread of the caller is going away (not just
597          * the structure) -- this is necessary for the module-remove case.
598          * This is important in preemption kernels, which transfer the flow
599          * of execution immediately upon a complete().
600          */
601         DBGPRINT(RT_DEBUG_TRACE, ("<---RTUSBCmdThread\n"));
602
603 #ifndef KTHREAD_SUPPORT
604         pTask->taskPID = THREAD_PID_INIT_VALUE;
605         complete_and_exit(&pTask->taskComplete, 0);
606 #endif
607         return 0;
608
609 }
610
611 void RTUSBWatchDog(struct rt_rtmp_adapter *pAd)
612 {
613         struct rt_ht_tx_context *pHTTXContext;
614         int idx;
615         unsigned long irqFlags;
616         PURB pUrb;
617         BOOLEAN needDumpSeq = FALSE;
618         u32 MACValue;
619         u32 TxRxQ_Pcnt;
620
621         idx = 0;
622         RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
623         if ((MACValue & 0xff) != 0) {
624                 DBGPRINT(RT_DEBUG_TRACE,
625                          ("TX QUEUE 0 Not EMPTY(Value=0x%0x)!\n",
626                           MACValue));
627                 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
628                 while ((MACValue & 0xff) != 0 && (idx++ < 10)) {
629                         RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
630                         RTMPusecDelay(1);
631                 }
632                 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
633         }
634
635         if (pAd->watchDogRxOverFlowCnt >= 2) {
636                 DBGPRINT(RT_DEBUG_TRACE,
637                          ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
638                 if ((!RTMP_TEST_FLAG
639                      (pAd,
640                       (fRTMP_ADAPTER_RESET_IN_PROGRESS |
641                        fRTMP_ADAPTER_BULKIN_RESET |
642                        fRTMP_ADAPTER_HALT_IN_PROGRESS |
643                        fRTMP_ADAPTER_NIC_NOT_EXIST)))) {
644                         DBGPRINT(RT_DEBUG_TRACE,
645                                  ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
646                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
647                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN,
648                                                 NULL, 0);
649                         needDumpSeq = TRUE;
650                 }
651                 pAd->watchDogRxOverFlowCnt = 0;
652         }
653
654         RTUSBReadMACRegister(pAd, 0x438, &TxRxQ_Pcnt);
655
656         for (idx = 0; idx < NUM_OF_TX_RING; idx++) {
657                 pUrb = NULL;
658
659                 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
660                 if ((pAd->BulkOutPending[idx] == TRUE)
661                     && pAd->watchDogTxPendingCnt) {
662                         int actual_length = 0, transfer_buffer_length = 0;
663                         BOOLEAN isDataPacket = FALSE;
664                         pAd->watchDogTxPendingCnt[idx]++;
665
666                         if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
667                             (!RTMP_TEST_FLAG
668                              (pAd,
669                               (fRTMP_ADAPTER_RESET_IN_PROGRESS |
670                                fRTMP_ADAPTER_HALT_IN_PROGRESS |
671                                fRTMP_ADAPTER_NIC_NOT_EXIST |
672                                fRTMP_ADAPTER_BULKOUT_RESET)))
673                             ) {
674                                 /* FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it! */
675                                 pHTTXContext =
676                                     (struct rt_ht_tx_context *)(&pAd->TxContext[idx]);
677                                 if (pHTTXContext->IRPPending) { /* Check TxContext. */
678                                         pUrb = pHTTXContext->pUrb;
679
680                                         actual_length = pUrb->actual_length;
681                                         transfer_buffer_length =
682                                             pUrb->transfer_buffer_length;
683                                         isDataPacket = TRUE;
684                                 } else if (idx == MGMTPIPEIDX) {
685                                         struct rt_tx_context *pMLMEContext, *pNULLContext,
686                                             *pPsPollContext;
687
688                                         /*Check MgmtContext. */
689                                         pMLMEContext =
690                                             (struct rt_tx_context *)(pAd->MgmtRing.
691                                                            Cell[pAd->MgmtRing.
692                                                                 TxDmaIdx].
693                                                            AllocVa);
694                                         pPsPollContext =
695                                             (struct rt_tx_context *)(&pAd->PsPollContext);
696                                         pNULLContext =
697                                             (struct rt_tx_context *)(&pAd->NullContext);
698
699                                         if (pMLMEContext->IRPPending) {
700                                                 ASSERT(pMLMEContext->
701                                                        IRPPending);
702                                                 pUrb = pMLMEContext->pUrb;
703                                         } else if (pNULLContext->IRPPending) {
704                                                 ASSERT(pNULLContext->
705                                                        IRPPending);
706                                                 pUrb = pNULLContext->pUrb;
707                                         } else if (pPsPollContext->IRPPending) {
708                                                 ASSERT(pPsPollContext->
709                                                        IRPPending);
710                                                 pUrb = pPsPollContext->pUrb;
711                                         }
712                                 }
713
714                                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx],
715                                                 irqFlags);
716
717                                 printk(KERN_INFO "%d:%lu LTL=%d , TL=%d L:%d\n",
718                                        idx, pAd->watchDogTxPendingCnt[idx],
719                                        pAd->TransferedLength[idx],
720                                        actual_length, transfer_buffer_length);
721
722                                 if (pUrb) {
723                                         if ((isDataPacket
724                                              && pAd->TransferedLength[idx] ==
725                                              actual_length
726                                              && pAd->TransferedLength[idx] <
727                                              transfer_buffer_length
728                                              && actual_length != 0
729 /*                                      && TxRxQ_Pcnt==0 */
730                                              && pAd->watchDogTxPendingCnt[idx] >
731                                              3)
732                                             || isDataPacket == FALSE
733                                             || pAd->watchDogTxPendingCnt[idx] >
734                                             6) {
735                                                 DBGPRINT(RT_DEBUG_TRACE,
736                                                          ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n",
737                                                           idx));
738                                                 DBGPRINT(RT_DEBUG_TRACE,
739                                                          ("Unlink the pending URB!\n"));
740                                                 /* unlink it now */
741                                                 RTUSB_UNLINK_URB(pUrb);
742                                                 /* Sleep 200 microseconds to give cancellation time to work */
743                                                 /*RTMPusecDelay(200); */
744                                                 needDumpSeq = TRUE;
745                                         }
746                                 } else {
747                                         DBGPRINT(RT_DEBUG_ERROR,
748                                                  ("Unknown bulkOut URB maybe hanged!\n"));
749                                 }
750                         } else {
751                                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx],
752                                                 irqFlags);
753                         }
754
755                         if (isDataPacket == TRUE)
756                                 pAd->TransferedLength[idx] = actual_length;
757                 } else {
758                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
759                 }
760         }
761
762         /* For Sigma debug, dump the ba_reordering sequence. */
763         if ((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0)) {
764                 u16 Idx;
765                 struct rt_ba_rec_entry *pBAEntry = NULL;
766                 u8 count = 0;
767                 struct reordering_mpdu *mpdu_blk;
768
769                 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
770
771                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
772                 if ((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL)) {
773                         DBGPRINT(RT_DEBUG_TRACE,
774                                  ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
775                         NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
776                         mpdu_blk = pBAEntry->list.next;
777                         while (mpdu_blk) {
778                                 DBGPRINT(RT_DEBUG_TRACE,
779                                          ("\t%d:Seq-%d, bAMSDU-%d!\n", count,
780                                           mpdu_blk->Sequence,
781                                           mpdu_blk->bAMSDU));
782                                 mpdu_blk = mpdu_blk->next;
783                                 count++;
784                         }
785
786                         DBGPRINT(RT_DEBUG_TRACE,
787                                  ("\npBAEntry->LastIndSeq=%d!\n",
788                                   pBAEntry->LastIndSeq));
789                         NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
790                 }
791         }
792 }
793
794 /*
795 ========================================================================
796 Routine Description:
797     Release allocated resources.
798
799 Arguments:
800     *dev                                Point to the PCI or USB device
801         pAd                                     driver control block pointer
802
803 Return Value:
804     None
805
806 Note:
807 ========================================================================
808 */
809 static void rt2870_disconnect(struct usb_device *dev, struct rt_rtmp_adapter *pAd)
810 {
811         DBGPRINT(RT_DEBUG_ERROR,
812                  ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
813                   dev->bus->bus_name, dev->devpath));
814         if (!pAd) {
815                 usb_put_dev(dev);
816                 printk("rtusb_disconnect: pAd == NULL!\n");
817                 return;
818         }
819         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
820
821         /* for debug, wait to show some messages to /proc system */
822         udelay(1);
823
824         RtmpPhyNetDevExit(pAd, pAd->net_dev);
825
826         /* FIXME: Shall we need following delay and flush the schedule?? */
827         udelay(1);
828         flush_scheduled_work();
829         udelay(1);
830
831         /* free the root net_device */
832         RtmpOSNetDevFree(pAd->net_dev);
833
834         RtmpRaDevCtrlExit(pAd);
835
836         /* release a use of the usb device structure */
837         usb_put_dev(dev);
838         udelay(1);
839
840         DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
841 }
842
843 static int __devinit rt2870_probe(IN struct usb_interface *intf,
844                                   IN struct usb_device *usb_dev,
845                                   IN const struct usb_device_id *dev_id,
846                                   struct rt_rtmp_adapter **ppAd)
847 {
848         struct net_device *net_dev = NULL;
849         struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)NULL;
850         int status, rv;
851         void *handle;
852         struct rt_rtmp_os_netdev_op_hook netDevHook;
853
854         DBGPRINT(RT_DEBUG_TRACE, ("===>rt2870_probe()!\n"));
855
856         /* Check chipset vendor/product ID */
857         /*if (RT28XXChipsetCheck(_dev_p) == FALSE) */
858         /*      goto err_out; */
859
860 /*RtmpDevInit============================================= */
861         /* Allocate struct rt_rtmp_adapter adapter structure */
862         handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
863         if (handle == NULL) {
864                 printk
865                     ("rt2870_probe(): Allocate memory for os handle failed!\n");
866                 return -ENOMEM;
867         }
868         ((struct os_cookie *)handle)->pUsb_Dev = usb_dev;
869
870         rv = RTMPAllocAdapterBlock(handle, &pAd);
871         if (rv != NDIS_STATUS_SUCCESS) {
872                 kfree(handle);
873                 goto err_out;
874         }
875 /*USBDevInit============================================== */
876         if (USBDevConfigInit(usb_dev, intf, pAd) == FALSE)
877                 goto err_out_free_radev;
878
879         RtmpRaDevCtrlInit(pAd, RTMP_DEV_INF_USB);
880
881 /*NetDevInit============================================== */
882         net_dev = RtmpPhyNetDevInit(pAd, &netDevHook);
883         if (net_dev == NULL)
884                 goto err_out_free_radev;
885
886         /* Here are the net_device structure with usb specific parameters. */
887         /* for supporting Network Manager.
888          * Set the sysfs physical device reference for the network logical device if set prior to registration will
889          * cause a symlink during initialization.
890          */
891         SET_NETDEV_DEV(net_dev, &(usb_dev->dev));
892
893         pAd->StaCfg.OriDevType = net_dev->type;
894
895 /*All done, it's time to register the net device to linux kernel. */
896         /* Register this device */
897         status = RtmpOSNetDevAttach(net_dev, &netDevHook);
898         if (status != 0)
899                 goto err_out_free_netdev;
900
901 #ifdef KTHREAD_SUPPORT
902         init_waitqueue_head(&pAd->mlmeTask.kthread_q);
903         init_waitqueue_head(&pAd->timerTask.kthread_q);
904         init_waitqueue_head(&pAd->cmdQTask.kthread_q);
905 #endif
906
907         *ppAd = pAd;
908
909         DBGPRINT(RT_DEBUG_TRACE, ("<===rt2870_probe()!\n"));
910
911         return 0;
912
913         /* --------------------------- ERROR HANDLE --------------------------- */
914 err_out_free_netdev:
915         RtmpOSNetDevFree(net_dev);
916
917 err_out_free_radev:
918         RTMPFreeAdapter(pAd);
919
920 err_out:
921         *ppAd = NULL;
922
923         return -1;
924
925 }