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